package GHM;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;

public class Game extends State{
	private Main main;
	private SpriteBatch spriteBatch;
	private OrthographicCamera gameCam;
	private OrthographicCamera guiCam;
	private Block[][] blocks;
	private Block[][] floor;
	private Character character;

	private Sprite moveLeft;
	private Sprite moveRight;
	private Sprite moveUp;
	private Sprite moveDown;
	public Game(Main m) {
		main = m;
		moveLeft = new Sprite(StaticTextures.moveLeft,0,0,64,64);
		moveRight = new Sprite(StaticTextures.moveRight,0,0,64,64);
		moveDown = new Sprite(StaticTextures.moveDown,0,0,64,64);
		moveUp = new Sprite(StaticTextures.moveUp,0,0,64,64);
		spriteBatch = new SpriteBatch();
		gameCam = new OrthographicCamera();
		gameCam.position.set(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),0);
		gameCam.zoom = 2f;
		guiCam = new OrthographicCamera();
		guiCam.position.set(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),0);
		guiCam.zoom = 2f;
		loadLevel(1);

		moveLeft.setPosition(Gdx.graphics.getWidth()*2f-64*3,64);
		moveRight.setPosition(Gdx.graphics.getWidth()*2f-64,64);
		moveDown.setPosition(Gdx.graphics.getWidth()*2f-64*2,0);
		moveUp.setPosition(Gdx.graphics.getWidth()*2f-64*2,64+64);
	}
	public void loadLevel(int level) {
		blocks = new Block[10][10];
		floor = new Block[10][10];
		blocks[4][3] = new MoveBlock();
		for (int i = 0; i < blocks.length; i++) {
			for (int j = 0; j < blocks[0].length;j++) {
				if (i == 0 || j == 0 || i == blocks.length-1 || j == blocks[0].length-1) {
					blocks[j][i] = new Block_Wall();
				}
			}
		}
		blocks[1][2] = new Block_Wall();
		blocks[2][2] = new Block_Wall();
		blocks[4][1] = new Block_Wall();
		blocks[4][2] = new Block_Wall();
		
		
		
		for (int i = 0; i < blocks.length; i++) {
			for (int j = 0; j < blocks[0].length;j++) {
				if (blocks[j][i] instanceof Block_Wall) {
					((Block_Wall) blocks[j][i]).initImage(blocks, i, j);
				}
			}
		}
		character = new Character(1,1);
	}
	public void update() {
		gameCam.viewportHeight = Gdx.graphics.getHeight();
		gameCam.viewportWidth = Gdx.graphics.getWidth();
		guiCam.viewportHeight = Gdx.graphics.getHeight();
		guiCam.viewportWidth = Gdx.graphics.getWidth();
		GL10 gl = Gdx.graphics.getGL10();
		gl.glViewport(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
		gameCam.update();
		gameCam.apply(gl);
		guiCam.update();
		guiCam.apply(gl);
	}


	public void render() {
		spriteBatch.setProjectionMatrix(gameCam.combined);
		spriteBatch.begin();
		for (int i = 0; i < floor.length; i++) {
			for (int j = 0; j < floor[0].length;j++) {
				if (floor[j][i] != null) {
					floor[j][i].render(spriteBatch,i*64,j*64);
				} else {

				}
			}
		}
		for (int i = 0; i < blocks.length; i++) {
			for (int j = 0; j < blocks[0].length;j++) {
				if (blocks[j][i] != null) {
					blocks[j][i].render(spriteBatch,i*64,j*64);
				} else {

				}
			}
		}
		character.render(spriteBatch,character.getX()*64,character.getY()*64);
		spriteBatch.end();
		spriteBatch.setProjectionMatrix(guiCam.combined);
		spriteBatch.begin();
		moveLeft.draw(spriteBatch,0.4f);
		moveRight.draw(spriteBatch,0.4f);
		moveUp.draw(spriteBatch,0.4f);
		moveDown.draw(spriteBatch,0.4f);
		spriteBatch.end();
	}


	public void enter() {
		Gdx.input.setInputProcessor(this);

	}

	public boolean keyDown(int arg0) {
		if (arg0 == Input.Keys.LEFT) {
			moveChar(-1,0);
		} else if (arg0 == Input.Keys.RIGHT) {
			moveChar(1,0);
		} else if (arg0 == Input.Keys.DOWN) {
			moveChar(0,-1);
		} else if (arg0 == Input.Keys.UP) {
			moveChar(0,1);
		}
		return false;
	}


	public boolean keyTyped(char arg0) {

		return false;
	}


	public boolean keyUp(int arg0) {

		return false;
	}


	public boolean scrolled(int arg0) {

		return false;
	}
	public void moveChar(int movex, int movey) {
		int curX = character.getX();
		int curY = character.getY();
		if (blocks[curY+movey][curX+movex] == null) {
			character.setX(curX+movex);
			character.setY(curY+movey);
		} else if (blocks[curY+movey][curX+movex] instanceof MoveBlock) {
			if (moveBlock(blocks[curY+movey][curX+movex],curX+movex,curY+movey,movex,movey)) {
				moveChar(movex,movey);
			}
		}
	}
	public boolean moveBlock(Block block, int fromx, int fromy, int movex, int movey) {
		if (block instanceof MoveBlock) {
			if (blocks[fromy+movey][fromx+movex] == null) {
				blocks[fromy+movey][fromx+movex] = block;
				blocks[fromy][fromx] = null;
				return true;
			}
		}
		return false;
	}
	public boolean touchDown(int x, int y, int arg2, int arg3) {
		Vector3 vec = new Vector3();
		vec.x = x;
		vec.y = y;
		guiCam.unproject(vec);
		if (moveLeft.getBoundingRectangle().contains(vec.x,vec.y)) {
			moveChar(-1,0);
		} else if (moveRight.getBoundingRectangle().contains(vec.x,vec.y)) {
			moveChar(1,0);
		} else if (moveDown.getBoundingRectangle().contains(vec.x,vec.y)) {
			moveChar(0,-1);
		} else if (moveUp.getBoundingRectangle().contains(vec.x,vec.y)) {
			moveChar(0,1);
		}
		return false;
	}


	public boolean touchDragged(int arg0, int arg1, int arg2) {

		return false;
	}


	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}


	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {

		return false;
	}
}
